<?php

namespace App\Exports\Stats\Platform;

use App\Exports\Export;

class SettlementExport extends Export
{

    /**
     * 映射标题行
     *
     * @return array
     */
    protected function headings(): array
    {
        $headings = trans('view.stats.platform.settlement.headings');

        // 删除列【游戏】
        if ($this->type == 'all') {
            unset($headings['game_id']);
        }
        // 删除列【场景】
        if ($this->type != 'gameScene') {
            unset($headings['scene_id']);
        }

        return $headings;
    }

    /**
     * 映射数据行
     *
     * @param \Illuminate\Database\Eloquent\Model $item
     * @return array
     */
    protected function map($item): array
    {
        $data = [
            timestamp_to_datetime($item->timed, 'day'),
            $item->org->name ?? $item->plat_code,
        ];

        if ($this->type != 'all') {
            $data[] = $this->games[$item->game_id] ?? $item->game_id;
        }
        if ($this->type == 'gameScene') {
            $data[] = $item->scene_id;
        }

        return array_merge($data, [
            $item->currency,
            $item->bet_base,
            $item->bet_num,
            $item->gain_player,
            $item->amount,
            $item->doc_count,
            $item->player_count,
            $item->usd['bet_base'],
            $item->usd['bet_num'],
            $item->usd['gain_player'],
            $item->usd['amount'],
        ]);
    }

    /**
     * 导出类型
     *
     * @var string
     */
    protected $type;

    /**
     * 游戏
     *
     * @var array
     */
    protected $games;

    /**
     * 初始化操作
     */
    protected function init()
    {
        // 文件名称
        $this->filename = trans('view.stats.platform.settlement.filename');

        // 导出类型
        if ($this->request->is('*/exportAll')) {
            $this->type = 'all';
        } elseif ($this->request->is('*/exportGame')) {
            $this->type = 'game';
        } elseif ($this->request->is('*/exportGameScene')) {
            $this->type = 'gameScene';
        }

        // 游戏
        $this->games = trans('view.game.games');
    }
}
